package com.softlab.retailwholesalesystem.controller;

import com.softlab.retailwholesalesystem.entity.*;
import com.softlab.retailwholesalesystem.entity.VO.PurchaseGoodsVOEntity;
import com.softlab.retailwholesalesystem.service.IDetailPurchaseService;
import com.softlab.retailwholesalesystem.service.exception.UpdateErrorException;
import com.softlab.retailwholesalesystem.util.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/purchases/detail")
public class DetailPurchaseController {
    @Autowired
    private IDetailPurchaseService service;

    @GetMapping("{id}")
    public JsonResult<List<PurchaseGoodsVOEntity>> get(@PathVariable Long id){
        List<PurchaseGoodsVOEntity> list = service.getMap(id);
        return new JsonResult<>(200,"查询采购单详情成功",list);
    }

    @PostMapping
    public JsonResult<Void> add(@RequestBody DetailPurchaseEntity map){
        service.save(map);
        return new JsonResult<>(200,"采购单详情添加成功");
    }

    @DeleteMapping("{id}")
    public JsonResult<Void> delete(@PathVariable Long id){
        DetailPurchaseEntity entity = new DetailPurchaseEntity();
        entity.setIsDelete(1);
        entity.setPurchaseMapId(id);
        service.updateById(entity);
        return new JsonResult<>(200,"采购单详情删除成功");
    }

    @PutMapping
    public JsonResult<Void> update(@RequestBody DetailPurchaseEntity map){
        boolean b = service.updateById(map);
        if (b){
            throw new UpdateErrorException("修改时遇到错误");
        }
        return new JsonResult<>(200,"修改成功");
    }

    @GetMapping("/search")
    public JsonResult<List<PurchaseGoodsVOEntity>> getPurchaseById(PurchaseEntity entity){
        List<PurchaseGoodsVOEntity> list = service.search(entity);
        return new JsonResult<>(200, "采购单查找成功", list);
    }
}
